var planId;
var mode = 0; //0-管理  1-添加

var templateParams = {
    tableTheads:["测试集名称", "测试用例", "创建时间", "测试数据","备注", "操作"],
    btnTools:[{
        type:"primary",
        size:"M",
        id:"manger-set",
        iconFont:"&#xe60c;",
        name:"管理测试集"
    },{
        size:"M",
        id:"add-set",
        iconFont:"&#xe600;",
        name:"添加测试集"
    },{
        size:"M",
        id:"batch-op",
        iMarkClass:"Hui-iconfont-del3",
        name:"批量操作"
    }]
};

var columnsSetting = [
    {
        "data": null,
        "render": function (data, type, full, meta) {
            return checkboxHmtl(data.set_name, data.id, "selectCaseSet");
        }
    },
    ellipsisData("set_name"),
    {
        "data": "case_count",
        "render": function (data, type, full, meta) {
            var context =
                [{
                    type: "default",
                    size: "M",
                    markClass: "show-set-case",
                    name: data || 0
                }];
            return btnTextTemplate(context);
        }
    },
    ellipsisData("create_time"),
    ellipsisSettingData('testdata_set', 'setting-test-data'),
    longTextData("mark", "set_name", "备注信息"),
    {
        "data": null,
        "render": function (data, type, full, meta) {
            var context;
            //管理
            if (mode == 0) {
                context = [{
                    title: "删除",
                    markClass: "del-plan-set",
                    iconFont: "&#xe6e2;"
                }];
            }

            //添加
            if (mode == 1) {
                context = [{
                    title: "添加",
                    markClass: "add-plan-set",
                    iconFont: "&#xe600;"
                }];
            }
            return btnIconTemplate(context);
        }
    }
];


var eventList = {
    //显示测试集用例
    ".show-set-case":function () {
        let data = table.row( $(this).parents('tr') ).data();
        parent.layer_show(data.set_name + "-测试用例管理", "webSetCase.html?set_id=" + data.id, "86%", "90%"
            , 2, null, null, function(){
                refreshTable();
            });
    },
    "#batch-op":function() { //批量操作 删除或者添加
        let checkboxList = $(".selectCaseSet:checked");
        if (checkboxList.length < 1) {
            return false;
        }

        let opName = "删除";
        let url = REQUEST_URL.WEB_TEST_PLAN.delSetFromPlan + '&plan_id=' + planId;
        let sendData = [];
        if (mode == 1) {
            opName = "添加";
            url = REQUEST_URL.WEB_TEST_PLAN.addSet2Plan + '&plan_id=' + planId;
        }

        $.each(checkboxList, function(i, n){
            let data = table.row( $(n).parents('tr') ).data();
            if (mode == 1) {
                sendData.push(data.id);
            } else {
                sendData.push(data.map_id);
            }
        });

        layer.confirm('确定' + opName + '选中的' + checkboxList.length + '项吗？', {icon: 0, title: '提示'}, function(index){
            layer.close(index);
            GLOBAL_UTILS.ajaxUtils.ajaxPost(url, sendData, function(data){
                refreshTable();
                layer.msg(opName + '成功！', {icon: 1, time: 1600});
            });
        });
    },
    "#manger-set":function() { //管理测试集
        var that = this;
        mode = 0;
        refreshTable(REQUEST_URL.WEB_TEST_PLAN.listPlanSet + "&plan_id=" + planId, function(json) {
            $(that).addClass('btn-primary').siblings("#add-set").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-add").addClass("Hui-iconfont-del3");
    },
    "#add-set":function() { //添加测试集
        var that = this;
        mode = 1;
        refreshTable(REQUEST_URL.WEB_CASE_SET.listSet, function(json) {
            $(that).addClass('btn-primary').siblings("#manger-set").removeClass('btn-primary');
        }, null, true);
        $("#batch-op").children("i").removeClass("Hui-iconfont-del3").addClass("Hui-iconfont-add");
    },
    //从计划中删除
    ".del-plan-set":function(){
        var data = table.row( $(this).parents('tr') ).data();
        var that = this;
        layer.confirm('确定要从计划中删除此测试集吗?', {icon:0, title:'警告'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_TEST_PLAN.delSetFromPlan + '&plan_id=' + planId, [data.map_id], function(data){
                table.row($(that).parents('tr')).remove().draw();
                layer.msg('删除成功!', {icon:1, time:1500});
            });
        });
    },
    //添加到执行组
    ".add-plan-set": function() {
        var data = table.row( $(this).parents('tr') ).data();
        layer.confirm('确定添加此测试集到测试计划中吗?', {icon:0, title:'提示'}, function(index) {
            layer.close(index);

            GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_TEST_PLAN.addSet2Plan + '&plan_id=' + planId, [data.id] ,function(data){
                layer.msg('添加成功!', {icon:1, time:1500});
                refreshTable();
            });
        });
    },
    '.setting-test-data':function(){
        let data = table.row( $(this).parents('tr') ).data();
        if (GLOBAL_UTILS.stringUtils.isBlank(data.testdata_set)
            || !GLOBAL_UTILS.jsonUtils.isJsonStr(data.testdata_set)) {
            data.testdata_set = "{}";
        }
        layer_show('配置测试数据', templates['web-test-config-view']({default_data: JSON.parse(data.testdata_set)}), 580, 450, 1, function(layero, index){
            $(layero).find('#update-web-test-config').one('click', function(){  //更新配置
                let configObj = {};
                $("#web-test-config-variables").children('.row').each(function(i){
                    let key = $(this).find('input').eq(0).val();
                    let value = $(this).find('input').eq(1).val();
                    if (strIsNotEmpty(key) && strIsNotEmpty(value)) {
                        configObj[key] = value;
                    }
                });
                layer.close(index);
                if (!CompareJsonObj(configObj, JSON.parse(data.testdata_set))) {
                    GLOBAL_UTILS.ajaxUtils.ajaxPost(REQUEST_URL.WEB_TEST_PLAN.updateSetData, {map_id:data.map_id, testdata_set:JSON.stringify(configObj)}, function(json) {
                        layer.msg('更新成功!', {icon:1, time: 1500});
                        refreshTable();
                    });
                } else {
                    layer.msg('未有改动!', {icon:5, time:1800});
                }
            });
        }, null, null, {shadeClose:false, offset: '10px'});
    },
    "#web-test-config-add-variable":function(){//增加一对key-value
        $("#web-test-config-variables").append('<div class="row cl"><div class="form-label col-xs-5 col-sm-5">'
            + '<input type="text" class="input-text radius"></div>'
            + '<div class="formControls col-xs-5 col-sm-5"><input type="text" class="input-text radius"></div>'
            + '<div class="formControls col-xs-2 col-sm-2"><a class="btn btn-default radius">'
            + '<i class="Hui-iconfont">&#xe60b;</i></a></div></div>');
    },
    "#web-test-config-clear-all-varibale":function(){//清除全部
        layer.confirm('确认删除下列全部的测试数据吗？', {title:'警告'}, function(index){
            $("#web-test-config-variables").html('');
            layer.msg('全部删除成功!', {icon:1, time:1800});
            layer.close(index);
        });
    },
    "#web-test-config-variables a":function(){//删除该项
        var that = this;
        layer.confirm('确认删除该项测试数据吗?', {title:'警告'}, function(index) {
            $(that).parent('.formControls').parent('.row').remove();
            layer.msg('删除成功!', {icon:1, time:1800});
            layer.close(index);
        });
    }
};


var mySetting = {
    eventList:eventList,
    templateCallBack:function(df){
        planId = GetQueryString("plan_id");
        publish.renderParams.listPage.listUrl = REQUEST_URL.WEB_TEST_PLAN.listPlanSet + "&plan_id=" + planId;

        df.resolve();
    },
    listPage:{
        tableObj:".table-sort",
        columnsSetting:columnsSetting,
        columnsJson:[0, 4, 5, 6],
        dtOtherSetting:{
            serverSide:false
        },
        exportExcel:false
    },
    templateParams:templateParams
};


$(function(){
    publish.renderParams = $.extend(true,publish.renderParams,mySetting);
    publish.init();
});
